package com.example.demo.service.impl;

import com.example.demo.compent.RedisUtil;
import com.example.demo.dao.UserInfoDao;
import com.example.demo.po.User;
import com.example.demo.service.UserAsynServiceI;
import com.example.demo.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserServiceI {
    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserInfoDao userInfoDao;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private UserAsynServiceI userAsynServiceI;


    @Override
    public User getByCardNo(String cardNo) {

        // 先从redis 读取,没有再从mysql 读取

        Object o = redisUtil.get(cardNo);

        if (o != null) {
            return (User) o;
        }
        User user = userInfoDao.getByCardNo(cardNo);

        if (user != null) {
            userAsynServiceI.add(user);
        }
        LOG.info("add ");
        return user;

    }

    @Override
    public boolean add(User user) {
        return userInfoDao.insert(user.getName(), user.getCardNo(), user.getPhoneNo(), user.getHealthStatus());

    }


}
